﻿using UnityEngine;

//攻击范围检测
public class AttackRangeDetect
{

    //扇形攻击范围
    public static bool AttackRangeSector(Transform attacker, Transform target, float SKillDistance, float skillAngle)
    {
        //与敌人的距离
        float distance = Vector3.Distance(attacker.position, target.position);
        //玩家正前方的向量
        Vector3 norVec = attacker.rotation * Vector3.forward;
        //玩家与敌人的方向向量
        Vector3 temVec = target.position - attacker.position;
        //两个向量的夹角
        float angle = Mathf.Acos(Vector3.Dot(norVec.normalized, temVec.normalized)) * Mathf.Rad2Deg;
        if (distance < SKillDistance && angle <= skillAngle * 0.5)
            return true;
        return false;
    }

    //长方形攻击范围
    public static bool AttackRangeRect(Transform attacker, Transform target, float length, float width)
    {
        //与敌人的距离
        //float distance = Vector3.Distance(attacker.position, target.position);
        //玩家与敌人的方向向量
        Vector3 temVec = target.position - attacker.position;
        //与玩家正前方做点积
        float forwardDis = Vector3.Dot(temVec, attacker.forward.normalized);
        if(forwardDis > 0 && forwardDis <= length)
        {
            float rightDistance = Vector3.Dot(temVec, attacker.right.normalized);
            if (Mathf.Abs(rightDistance) <= width / 2)
                return true;
        }
        return false;
    }

    //半圆形攻击范围
    public static bool AttackRangeSemicircle(Transform attacker, Transform target, float radius)
    {
        //玩家与敌人的方向向量
        Vector3 temVec = target.position - attacker.position;
        //与玩家正前方做点积
        float forwardDistance = Vector3.Dot(temVec, attacker.forward.normalized);
        
        if(forwardDistance > 0 && forwardDistance <= radius)
        {
            float rightDistance = Vector3.Dot(temVec, attacker.right.normalized);
            if (Mathf.Abs(rightDistance) <= radius)
                return true;
        }
        return false;
    }
}